import sys
from pathlib import Path
sys.path.append(str(Path(__file__).parent.parent))  # 添加项目根目录到Python路径

import unittest
import os
from modules.audio_processor import audio_processor

class TestAudioProcessor(unittest.TestCase):
    def setUp(self):
        # 创建测试用的临时文件
        self.test_video = os.path.join(os.path.dirname(__file__), "data", "test_movie.mp4")
        self.test_audio = os.path.join(os.path.dirname(__file__), "data", "test_audio.wav")
        self.test_text = "测试语音识别和合成功能"
        self.output_audio = os.path.join(os.path.dirname(__file__), "data", "output_audio.wav")

    def test_speech_to_text_funasr(self):
        print(self.test_audio)
        text = audio_processor.speech_to_text(self.test_audio)
        self.assertIsInstance(text, str)
        self.assertGreater(len(text), 0)  # 检查是否有文字
        print(text)
    
    def test_text_to_speech(self):
        success = audio_processor.text_to_speech(self.test_text, self.output_audio)
        # 播放生成的音频文件
        if os.path.exists(self.output_audio):
            import subprocess
            subprocess.run(["ffplay", self.output_audio])
        self.assertTrue(success)

    def tearDown(self):
        # 清理测试文件
        if os.path.exists(self.test_audio):
            pass
            # os.remove(self.test_audio)
        if os.path.exists(self.output_audio):
            pass
            # os.remove(self.output_audio)

if __name__ == "__main__":
    unittest.main()